Imports System.Text
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic
Imports System.Xml

Public Class Vendor

    Private Const XMLPath As String = "~/App_Data/Vendors.xml"
    Private Const XMLItemPath As String = "~/App_Data/Inventory.xml"

#Region " Public Properties"
    Public Property ID As Guid
    Public Property Name As String
    Public Property Address1 As String
    Public Property Address2 As String
    Public Property City As String
    Public Property State As String
    Public Property Zip As String
#End Region
#Region " Private XML Methods"
    Public Shared Function Search(ByVal vendorName As String) As List(Of Vendor)
        Dim vendorList As New List(Of Vendor)
        For Each i As Vendor In FetchList()
            If i.Name.ToLower.Contains(vendorName.ToLower) Then
                vendorList.Add(i)
            End If
        Next
        Return vendorList
    End Function
    Public Shared Function Delete(ByVal id As Guid, ByVal deleteChildren As Boolean, ByRef msgOut As String) As Boolean
        Dim _return As Boolean = False
        If deleteChildren Then
            _return = DeleteVendorAndChildren(id, msgOut)
        Else
            _return = DeleteVendorNotChildren(id, msgOut)
        End If
        Return _return
    End Function
    Public Shared Function FetchList(Optional ByVal AddNoVendor As Boolean = False) As List(Of Vendor)
        Dim vendorlist As New List(Of Vendor)
        Dim xdoc As New XmlDocument
        xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
        For Each n As XmlNode In xdoc("Vendors").ChildNodes
            vendorlist.Add(ConvertToObject(n))
        Next
        If AddNoVendor Then
            Dim noV As Vendor = New Vendor()
            noV.ID = New Guid("dbdbe57a-6F34-4FF7-a9c4-03eabda4fa95")
            noV.Name = " -- no vendor -- "
            vendorlist.Insert(0, noV)
        End If
        Return vendorlist
    End Function
    Public Shared Function Fetch(ByVal id As Guid) As Vendor
        Dim inv As New Vendor
        For Each i As Vendor In FetchList()
            If i.ID = id Then
                inv = i
                Exit For
            End If
        Next
        Return inv
    End Function
    Public Shared Function Save(ByVal v As Vendor) As Integer
        If Not v.ID = Nothing Then
            ' update
            Return Update(v)
        Else
            ' insert 
            Return Insert(v)
        End If
    End Function
    Private Shared Function ConvertToObject(ByVal n As XmlNode) As Vendor
        Dim _vendor As New Vendor
        With _vendor
            .ID = New Guid(n("ID").InnerText)
            .Address1 = n("Address1").InnerText
            .Address2 = n("Address2").InnerText
            .City = n("City").InnerText
            .Name = n("Name").InnerText
            .State = n("State").InnerText
            .Zip = n("Zip").InnerText
        End With
        Return _vendor
    End Function
    Private Shared Function Insert(ByVal v As Vendor) As Integer
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
            Dim n As XmlNode = xdoc("Vendors")
            Dim newNode As XmlNode = xdoc.CreateNode(XmlNodeType.Element, "Vendor", Nothing)
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "ID", Nothing)).InnerText = Guid.NewGuid.ToString
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Address1", Nothing)).InnerText = v.Address1
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Address2", Nothing)).InnerText = v.Address2
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "City", Nothing)).InnerText = v.City
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Name", Nothing)).InnerText = v.Name
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "State", Nothing)).InnerText = v.State
            newNode.AppendChild(xdoc.CreateNode(XmlNodeType.Element, "Zip", Nothing)).InnerText = v.Zip
            n.AppendChild(newNode)
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLPath))
            _return = True
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
        End Try
        Return _return
    End Function
    Private Shared Function Update(ByVal v As Vendor) As Integer
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
            For Each n As XmlNode In xdoc("Vendors").ChildNodes
                If New Guid(n("ID").InnerText) = v.ID Then
                    n("Address1").InnerText = v.Address1
                    n("Address2").InnerText = v.Address2
                    n("City").InnerText = v.City
                    n("Name").InnerText = v.Name
                    n("State").InnerText = v.State
                    n("Zip").InnerText = v.Zip
                    Exit For
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLPath))
            _return = True
        Catch ex As Exception
            'log error
        End Try
        Return _return
    End Function
    Private Shared Function DeleteVendorOnly(ByVal id As Guid, ByRef msgOut As String) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLPath))
            For Each n As XmlNode In xdoc("Vendors").ChildNodes
                If New Guid(n("ID").InnerText) = id Then
                    xdoc("Vendors").RemoveChild(n)
                    Exit For
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLPath))
            _return = True
        Catch ex As Exception
            ErrorLog.LogErrors(ex)
            msgOut += vbCrLf & "Delete Error: " & ex.Message
            _return = False
        End Try
        Return _return
    End Function
    Private Shared Function DeleteVendorNotChildren(ByVal id As Guid, ByRef msgOut As String) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLItemPath))
            For Each n As XmlNode In xdoc("Items").ChildNodes
                If New Guid(n("VendorID").InnerText) = id Then
                    n("VendorID").InnerText = Guid.Empty.ToString
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLItemPath))
            Return DeleteVendorOnly(id, msgOut)
        Catch ex As Exception
            msgOut += vbCrLf & ex.Message
        End Try
        Return _return
    End Function
    Private Shared Function DeleteVendorAndChildren(ByVal id As Guid, ByRef msgOut As String) As Boolean
        Dim _return As Boolean = False
        Try
            Dim xdoc As New XmlDocument
            xdoc.Load(HttpContext.Current.Server.MapPath(XMLItemPath))
            For Each n As XmlNode In xdoc("Items").ChildNodes
                If New Guid(n("VendorID").InnerText) = id Then
                    xdoc("Items").RemoveChild(n)
                End If
            Next
            xdoc.Save(HttpContext.Current.Server.MapPath(XMLItemPath))
            Return DeleteVendorOnly(id, msgOut)
        Catch ex As Exception
            msgOut += vbCrLf & ex.Message
        End Try
        Return _return
    End Function
#End Region
End Class
